package com.showtree


/**
  * Created by yc on 16-12-14.
  */

case class TreeNode[T](data:T,children:Seq[TreeNode[T]]=Nil)

object show {
  def asciiDisplay(root: TreeNode[String]): Seq[String] =
    if(root.children == Nil) "+-"+root.data  :: Nil
    else "+-"+root.data ::
      root.children.flatMap(x=>asciiDisplay(x))
        .map(x=>if(x.startsWith(" ") ) " |"
          +x.replace("|"," ") else " "+x).toList


  def main(args: Array[String]): Unit = {
   asciiDisplay(TreeNode("Root",
      children = List(TreeNode("level1-1"),
        TreeNode("level1-2"),
        TreeNode("level1-3")))).foreach(println)
   println()
    println()
   asciiDisplay(TreeNode("Root",
      children = List(
        TreeNode("level1-1", children = TreeNode("level2-1", children = TreeNode("level3-1") :: Nil) :: Nil),
        TreeNode("level1-2"),
        TreeNode("level1-3")))).foreach(println)
}


}

